package com.base.cn.platform.os.common.utils.web;

import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * 获取客户端的浏览器名称 版本；操作系统名称 版本；请求来源IP 所在地
 *
 * @author :xujunbao
 * @since 2014年11月4日 下午1:37:08
 */
public class UserAgentUtil {

    private HttpServletRequest request;
    private String userAgentString;
    private UserAgent userAgent;

    private final String verRegex = "\\/(([0-9]+)\\.?([\\w]+)?(\\.[\\w]+)?(\\.[\\w]+)?)";

    private UserAgentUtil() {
    }

    public UserAgentUtil(HttpServletRequest request) {
        this.request = request;
        this.userAgentString = request.getHeader("user-agent");
        userAgent = new UserAgent(userAgentString);
    }

    /**
     * 获得浏览器名称
     *
     * @return String 浏览器名
     */
    public String getBrowserName() {
        if (userAgentString != null) {
            if (userAgentString.contains("QQBrowser")) {
                return "QQ浏览器";
            } else if (userAgentString.contains("UBroswer")) {
                return "UC浏览器";
            } else if (userAgentString.contains("LBBROWSER")) {
                return "猎豹浏览器";
            } else if (userAgentString.contains("OPR")) {
                return "Opera浏览器";
            } else if (userAgentString.contains("360SE") || userAgentString.contains("QIHU")) {
                return "360浏览器";
            } else if (userAgentString.contains("SE") && userAgentString.contains("MetaSr")) {
                return "搜狗浏览器";
            } else {
                return userAgent.getBrowser().getName();
            }
        } else {
            return "Other";
        }
    }

    /**
     * 获得浏览器版本
     */
    public String getBrowserVersion() {
        if (userAgentString != null) {
            if (userAgentString.contains("QQBrowser")) {
                return Pattern.compile("QQBrowser" + verRegex).matcher(userAgentString).group();
            } else if (userAgentString.contains("UBrowser")) {
                return Pattern.compile("UBrowser" + verRegex).matcher(userAgentString).group();
            } else if (userAgentString.contains("LBBROWSER")) {
                return Pattern.compile("LLBROWSER" + verRegex).matcher(userAgentString).group();
            } else if (userAgentString.contains("OPR")) {
                return Pattern.compile("OPR" + verRegex).matcher(userAgentString).group();
            } else if (userAgentString.contains("360SE") || userAgentString.contains("QIHU")) {
                return Pattern.compile("360SE" + verRegex).matcher(userAgentString).group();
            } else if (userAgentString.contains("SE") && userAgentString.contains("MetaSr")) {
                return Pattern.compile("SE" + verRegex).matcher(userAgentString).group();
            } else {
                Version version = userAgent.getBrowserVersion();
                return version == null ? "" : version.getVersion();
            }
        } else {
            return "";
        }
    }

    /**
     * 根据ip获得用户的地理位置
     *
     * @return 用户的地理位置
     */
    public String getUserAddress() {
        try {
            String host = "https://dm-81.data.aliyun.com";
            String path = "/rest/160601/ip/getIpInfo.json";
            String params = "?ip=49.5.6.118";
            String appcode = "5a50bdd4e41f4af2abeaa6b60baf956c";
            /*if (entity != null) {
                return new JsonParser().parse(EntityUtils.toString(entity)).getAsJsonObject()
						.get("data").getAsJsonObject().get("region").getAsString();
			}*/
            return "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "unknown";
    }

    /**
     * 获得用户的ip
     */
    public String getUserIp() {
        return request.getRemoteAddr();
    }

    /**
     * 获得操作系统名
     */
    public String getOSName() {
        return userAgent.getOperatingSystem().getName();
    }

}
